//
//  HHLaderInPagesViewController.m
//  YSJ
//
//  Created by 花花 on 2016/12/15.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHLaderInPagesViewController.h"

@interface HHLaderInPagesViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property(nonatomic, strong) UICollectionView *collectionView;

//pageControl
@property(nonatomic, strong) UIPageControl *pageControl;

//共多少页
@property (nonatomic, assign)   NSInteger count;


@property(nonatomic, copy) finishbtnOnClick finishbtnOnClick;

@property(nonatomic, copy) setupCellHandler setupCellHandler;

@end

static NSString *const CellID = @"CellID";
@implementation HHLaderInPagesViewController

#pragma mark - Init
- (instancetype)initWithPagesCount:(NSInteger)count setupCellHandler:(setupCellHandler)setupCellHandler finishOnClick:(finishbtnOnClick)finishbtnOnClick{

    if (self  == [super initWithNibName:nil bundle:nil]) {
        
        _count = count;
        _setupCellHandler =[setupCellHandler copy];
        _finishbtnOnClick = [finishbtnOnClick copy];
        
        /**
         [self commonInit]; //如果内容多了就抽出一个方法出来
         在这里只需要添加一个collectionView和pageControl 所以就放在这里吧
         */
        //懒加载 ----初始化
        [self.view addSubview:self.collectionView];
        [self.view addSubview: self.pageControl];
        //注册cell
        [self.collectionView registerClass:[HHLaderPagesCell class] forCellWithReuseIdentifier:CellID];
        
    }
    
    return self;
  
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{


    NSUInteger currentIndex = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    
    if (self.pageControl.currentPage !=  currentIndex) {
        self.pageControl.currentPage = currentIndex;
    }

}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.count;

}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HHLaderPagesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    //如果滑到最后一页了，按钮显示 pageControl隐藏
    if (indexPath.row == self.count - 1) {
        
        cell.finishbtn.hidden = NO;
        self.pageControl.hidden = YES;
        [cell.finishbtn addTarget:self action:@selector(finishOnClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.finishbtn.hidden = YES;
        self.pageControl.hidden = NO;
        
   }
    
    
    if (self.setupCellHandler) {
        self.setupCellHandler(cell,indexPath);
    }

    return cell;
}

#pragma mark - SEL Method
-(void)finishOnClick:(UIButton *)btn{

    if (self.finishbtnOnClick) {
        self.finishbtnOnClick(btn);
    }

}

#pragma mark - Setter && Getter
-(UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = self.view.bounds.size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        UICollectionView *collectionView =[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        _collectionView = collectionView;
    }
    

    return _collectionView;
}

-(UIPageControl *)pageControl{

    if (!_pageControl) {
        UIPageControl *pageControl = [[UIPageControl alloc]init];
        pageControl.numberOfPages = self.count;
        pageControl.currentPage = 0;
        CGSize pageSize = [pageControl sizeForNumberOfPages:self.count];
        CGFloat pageControlX = (screen_width - pageSize.width)/2;
        CGFloat pageControlY = self.view.bounds.size.height -pageSize.height -50;
        pageControl.frame = CGRectMake(pageControlX, pageControlY, pageSize.width, pageSize.height);
        _pageControl = pageControl;
        
    }
    return _pageControl;
}

@end
